import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: [],
        admin: [],
        columns: [],
        switchAlliance: [],
        showAct: false,
        userUnionId: ""
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        let columns = [];
        await db.collection("administer").where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let array = [];
                for (let i of res.data) {
                    array.push(i.unionId);
                }
                This.setData({ admin: array });
            });
        await db.collection("sign-in").where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = res.data;
                for (i = 0; i < member.length; i++) {
                    for (j of this.data.admin) {
                        if (member[i].unionId == j) {
                            member.splice(i, 1);
                            i = 0;
                        }
                    }
                }
                This.setData({ member: member });
            });
        await db.collection('key')
            .get()
            .then((res) => {
                res.data.map((value, index) => {
                    columns.push(value.alliance);
                })
            })
        this.setData({ columns: columns });
        this.setData({ switchAlliance: columns[0] });
    },
    async switchAlliance(res) {
        this.setData({ showAct: true });
        this.setData({ userUnionId: res.target.dataset.id });
    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },
    onClickLeft() {
        this.setData({ showAct: false });
    },
    async onClickRight() {
        const db = qq.cloud.database();
        const _ = db.command;
        await db.collection("member")
            .where({
                unionId: this.data.userUnionId,
                alliance: app.data.alliance
            })
            .update({
                data: {
                    alliance: this.data.switchAlliance
                }
            })
            .then(res => {}).catch(err => {});
        await db.collection("sign-in")
            .where({
                unionId: this.data.userUnionId,
                alliance: app.data.alliance
            })
            .update({
                data: {
                    alliance: this.data.switchAlliance
                }
            })
            .then(res => {
                Toast('切换成功');
            }).catch(err => {
                Toast('切换失败')
            })
        await db.collection("sign-in").where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = res.data;
                for (i = 0; i < member.length; i++) {
                    for (j of this.data.admin) {
                        if (member[i].unionId == j) {
                            member.splice(i, 1);
                            i = 0;
                        }
                    }
                }
                this.setData({ member: member });
            });
        this.setData({ showAct: false });
    },
    onChangeAlliance(event) {
        const { picker, value, index } = event.detail;
        this.setData({ switchAlliance: value });
    },

})